#!/bin/bash

source /home/shell/util.sh



# 安装函数
function install () {
    if [ ! -f "/opt/zookeeper/bin/zkServer.sh" ]; then
        bash /home/shell/zookeeper-install.sh
    fi
    requireJava
    source /etc/profile.d/java.sh

    while [ true ]; do
        curl -sL https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/|grep -Po "<a href=\"\d+\.\d+\.\d+/\">"|perl -pe "s/<a href=\"(.*)\/\">/\1/g"
        read -p "请选择大版本：" big_version
        if [[ -z "${big_version}" ]]; then
        printFail "请输入版本"
        else
        break;
        fi
    done
    while [ true ]; do
        curl -sL https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/${big_version}/|grep -Po "<a href=\"kafka_.*.tgz\">"|grep -v "docs"|perl -pe "s/<a href=\"(.*?)\">/\1/g"
        read -p "请选择小版本：" version
        if [[ -z "${version}" ]]; then
        printFail "请输入版本"
        else
        break;
        fi
    done
    
    
    download /opt/kafka.tgz https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/${big_version}/${version}
    tar -xf /opt/kafka.tgz -C /opt
    rm -rf /opt/kafka.tgz
    mv /opt/kafka_* /opt/kafka
}

# 配置函数
function config(){

tee /etc/profile.d/kafka.sh<<EOF
export KAFKA_HOME=/opt/kafka
export PATH=$PATH:$KAFKA_HOME/bin
EOF
    source /etc/profile.d/kafka.sh

    read -p "broker_id[0]：" broker_id
    broker_id=${broker_id:-"0"}
    read -p "端口[9092]：" kafka_port
    kafka_port=${kafka_port:-"9092"}
    read -p "zookeeper地址[localhost:2181]：" zookeeper_addr
    zookeeper_addr=${zookeeper_addr:-"localhost:2181"}
    read -p "处理网络请求的线程数量[3]：" num_network_threads
    num_network_threads=${num_network_threads:-"3"}
    read -p "用来处理磁盘IO的线程数量[8]：" num_io_threads
    num_io_threads=${num_io_threads:-"8"}
    read -p "kafka运行日志存放的目录[/tmp/kafka-logs]：" log_dirs
    log_dirs=${log_dirs:-"/tmp/kafka-logs"}

    sed -ri "s@#listeners=PLAINTEXT://:9092@listeners=PLAINTEXT://:"${kafka_port}"@g" /opt/kafka/config/server.properties
    sed -ri "s@zookeeper.connect=localhost:2181@zookeeper.connect="${zookeeper_addr}"@g" /opt/kafka/config/server.properties
    sed -ri "s@num.network.threads=3@num.network.threads="${num_network_threads}"@g" /opt/kafka/config/server.properties
    sed -ri "s@num.io.threads=8@num.io.threads="${num_io_threads}"@g" /opt/kafka/config/server.properties
    sed -ri "s@log.dirs=/tmp/kafka-logs@log.dirs="${log_dirs}"@g" /opt/kafka/config/server.properties

    firewall-addport 9320
}

function run () {
    bash /opt/kafka/bin/kafka-server-start.sh -daemon /opt/kafka/config/server.properties
}

function test () {
    bash /opt/kafka/bin/kafka-topics.sh 
}

function sbtinstall () {
    # https://www.cnblogs.com/pansn/p/14248625.html
    while [ true ]; do
        curl -sL https://www.scala-sbt.org/download.html|grep -Po "sbt\s\d+\.\d+\.\d+"|head -5|perl -pe "s/sbt\s(.*)/\1/g"
        read -p "请选择sbt版本：" version
        if [[ -z "${version}" ]]; then
        printFail "请输入版本"
        else
        break;
        fi
    done
    mkdir -p /opt/scala/
    download /opt/scala/sbt.tgz https://hub.fastgit.org/sbt/sbt/releases/download/v${version}/sbt-${version}.tgz
    tar -xf /opt/scala/sbt.tgz -C /opt/scala/
    rm -rf /opt/scala/sbt.tgz
tee /opt/scala/sbt/sbt<<EOF
SBT_OPTS="-Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M"
java \$SBT_OPTS -jar /opt/scala/sbt/bin/sbt-launch.jar "$@"
EOF
    chmod u+x /opt/scala/sbt/sbt

tee /etc/profile.d/sbt.sh<<EOF
export SBT_HOME=/opt/scala/sbt/
export PATH=\$SBT_HOME/bin:\$PATH
EOF
    source /etc/profile.d/sbt.sh
    sbt sbtVersion
}

function kafka-manager-install () {
    git clone https://hub.fastgit.org/yahoo/kafka-manager.git /usr/local/src/kafa-manager/
    read -p "zookeeper地址[127.0.0.1]：" zookeeper_ip
    zookeeper_ip=${zookeeper_ip:-"127.0.0.1"}
    sed -ri "s/kafka-manager-zookeeper/"${zookeeper_ip}"/g" /usr/local/src/kafa-manager/conf/application.conf
}


# 运行安装和配置
install && config && run

kafka-manager-install